package com.thinkgem.jeesite.common.utils;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.*;

/**
 * Created by aulbrother on 2016/4/8.
 */
public class LOCALMAC{
    private static Logger log = LoggerFactory.getLogger(LOCALMAC.class);

    public static String getLocalIp(){
        String ip = "";
        String temIp = "";
        try{
            Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
            while(en.hasMoreElements()){
                NetworkInterface intf = en.nextElement();
                Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
                while(enumIpAddr.hasMoreElements()){
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if(!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && inetAddress.isSiteLocalAddress()){
                        temIp = inetAddress.getHostAddress();
                        break;
                    }
                }
            }
        }
        catch(SocketException e){
            log.error("获取IP地址错误:{}", ExceptionUtils.getStackTrace(e));
        }
        if(StringUtils.isEmpty(ip)){
            ip = temIp;
        }
        return ip;
    }

    /**
     * 获取机器一个有效的MAC
     *
     * @return MAC列表
     */
    public static String getLocalOneMac(){
        List<String> localMac = getLocalMac();
        if(CollectionUtils.isEmpty(localMac)){
            return "";
        }
        else{
            return localMac.get(0);
        }
    }

    /**
     * 是否包含mac
     *
     * @return MAC列表
     */
    public static boolean contains(String tem){
        List<String> localMac = getLocalMac();
        for(String mac : localMac){
            if(tem.contains(mac)){
                return true;
            }
        }
        return false;
    }

    /**
     * 获取机器所有有效的MAC
     *
     * @return MAC列表
     */
    public static List<String> getLocalMac(){
        List<String> mac = new ArrayList<>();
        try {
            Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
            while (enumeration.hasMoreElements()) {
                StringBuilder stringBuffer = new StringBuilder();
                NetworkInterface networkInterface = enumeration.nextElement();
                if (networkInterface != null) {
                    byte[] bytes = networkInterface.getHardwareAddress();
                    if (bytes != null) {
                        for (int i = 0; i < bytes.length; i++) {
                            if (i != 0) {
                                stringBuffer.append("-");
                            }
                            int tmp = bytes[i] & 0xff; // 字节转换为整数
                            String str = Integer.toHexString(tmp);
                            if (str.length() == 1) {
                                stringBuffer.append("0").append(str);
                            } else {
                                stringBuffer.append(str);
                            }
                        }
                        mac.add(stringBuffer.toString().toUpperCase());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mac;
    }
}